/* $Id: ServiceDelegateImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.delegate.provider;

import java.util.List;

import ae.pcfc.etks.online.common.delegate.ServiceDelegate;
import ae.pcfc.etks.online.common.exception.AdminWorkflowException;
import ae.pcfc.etks.online.common.facade.AppMetaDataFacade;
import ae.pcfc.etks.online.common.facade.ServiceFacade;
import ae.pcfc.etks.online.common.vo.AdminApprovalVO;
import ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO;
import ae.pcfc.etks.online.common.vo.ClientRoleVO;
import ae.pcfc.etks.online.common.vo.DepartmentVO;
import ae.pcfc.etks.online.common.vo.PagingVO;
import ae.pcfc.etks.online.common.vo.ProcessVO;
import ae.pcfc.etks.online.common.vo.ServiceRoleVO;
import ae.pcfc.etks.online.common.vo.ServiceVO;
import ae.pcfc.etks.online.common.vo.UserServiceVO;
import ae.pcfc.etks.online.common.vo.UserVO;
import ae.pcfc.etks.online.wf.ConstantsWF;
import ae.pcfc.etks.online.wf.service.client.UserServiceWF;
import ae.pcfc.etks.online.wf.service.client.vo.CompleteStepVO;
import ae.pcfc.etks.online.wf.service.client.vo.UserServiceCompleteStepVO;
import ae.pcfc.etks.online.wf.service.client.vo.UserServiceLaunchIncidentVO;

public class ServiceDelegateImpl extends BaseDelegateImpl implements ServiceDelegate {
    	/*
	 * Here we coordinate between the Facade service which handles DB entities
	 * and the WF both will be looked up with service locator normally we will
	 * have 1-1 relationship between the delegate and the facade and 1-1
	 * relationship between the delegate and the WF service which means for
	 * CompanyRegistrationDelegate there will be ClientRegistrationFacade &
	 * CompanyRegistrationWF validations that related to DB vs WF are maintained
	 * here ie user cannot issue a new request while he already have a request
	 * in the wf Exception are translated in to business exception
	 */
	/**
	 * 
	 */
	public ServiceDelegateImpl() {

	}

	/**
	 * serviceListRegistration
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#serviceListRegistration(ae.pcfc.etks.online.common.vo.UserServiceVO)
	 * 
	 */
	public boolean serviceListRegistration(UserServiceVO userServiceVO) {

		return serviceLocator.getFacadeService(ServiceFacade.class).registerUserServiceList(userServiceVO);
	}

	/**
	 * serviceRegistration
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#serviceRegistration(ae.pcfc.etks.online.common.vo.ServiceVO)
	 * 
	 */
	public boolean serviceRegistration(ServiceVO serviceVO) {

		return serviceLocator.getFacadeService(ServiceFacade.class).registerService(serviceVO);
	}
	/**
	 * serviceUpdate
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#serviceUpdate(ae.pcfc.etks.online.common.vo.ServiceVO)
	 * 
	 */
	public boolean serviceUpdate(ServiceVO serviceVO) {

		return serviceLocator.getFacadeService(ServiceFacade.class).serviceUpdate(serviceVO);
	}

	/**
	 * deleteServiceById
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#deleteServiceById(java.lang.Long)
	 * 
	 */
	public boolean deleteServiceById(Long Id) {

		return serviceLocator.getFacadeService(ServiceFacade.class).deleteServiceById(Id);
	}

	/**
	 * getServicesList
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#getServicesList()
	 * 
	 */
	public List<ServiceVO> getServicesList() {
		return serviceLocator.getFacadeService(ServiceFacade.class).getServicesList();
	}

	/**
	 * getServiceByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#getServiceByPrimaryKey(java.lang.Long)
	 * 
	 */
	public ServiceVO getServiceByPrimaryKey(Long Id) {
		return serviceLocator.getFacadeService(ServiceFacade.class).getServiceByPrimaryKey(Id);
	}

	/**
	 * findServiceByUserNameAndTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceByUserNameAndTrakheesId(java.lang.String, java.lang.String)
	 * 
	 */
	public List<ServiceVO> findServiceByUserNameAndTrakheesId(String userName, String trakheesId) {
		return serviceLocator.getFacadeService(ServiceFacade.class).findServiceByUserNameAndTrakheesId(userName, trakheesId);
	}

	/**
	 * addServiceReq
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#addServiceReq(java.lang.String, java.util.List, ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO)
	 * 
	 */
	public String addServiceReq(String userName, List<ServiceVO> serviceVOs, AppMetaDataDetailVO appMetaDataVO) {

		String clientReferenceNumber = null;
		ServiceFacade serviceFacade = serviceLocator.getFacadeService(ServiceFacade.class);
	    AppMetaDataFacade appMetaDataFacade = serviceLocator.getFacadeService(AppMetaDataFacade.class);
	    UserServiceWF userServiceWF = serviceLocator.getWorkflowService(UserServiceWF.class);
	    String applicationID = appMetaDataFacade.getNextApplicationId();
	    appMetaDataVO.setStrApplicationId(applicationID);
	    
	    UserServiceLaunchIncidentVO launchIncidVO = new UserServiceLaunchIncidentVO();
		launchIncidVO.setgApplicationID(appMetaDataVO.getStrApplicationId());

		serviceFacade.updateWorkflowIncidentVO(launchIncidVO, serviceVOs);
		
		Integer wfIncidentId = userServiceWF.launchNonClientWF(launchIncidVO);
		appMetaDataVO.setStrWFIncidentId(wfIncidentId.toString());
	    
	    try{
			appMetaDataVO.setStrWFIncidentId(wfIncidentId.toString());
			clientReferenceNumber = serviceFacade.addServiceReq(userName, serviceVOs, appMetaDataVO);
	    }catch(RuntimeException t){
			CompleteStepVO completeStepVO = new CompleteStepVO();
			completeStepVO.setStrUserName(ConstantsWF.ULTIMUS_DEFAULT_INITIATOR_NAME);
			completeStepVO.setnIncidentNumber(wfIncidentId);
			completeStepVO.setStrSummary(ConstantsWF.ROLLBACK_INCIDENT);
			userServiceWF.abortIncident(completeStepVO);
		    	throw t;
	    }
	    	
	    return clientReferenceNumber;

	}

	/**
	 * findServiceListByIds
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceListByIds(java.util.List)
	 * 
	 */
	public List<ServiceVO> findServiceListByIds(List<Long> ids) {

		return serviceLocator.getFacadeService(ServiceFacade.class).findServiceListByIds(ids);
	}

	/**
	 * findServiceRequestByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceRequestByApplicationId(java.lang.String)
	 * 
	 */
	public List<ServiceVO> findServiceRequestByApplicationId(String applicationId) {

		return this.serviceLocator.getFacadeService(ServiceFacade.class).findServiceRequestByApplicationId(applicationId);
	}

	/**
	 * reviewRegisterService
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#reviewRegisterService(ae.pcfc.etks.online.common.vo.AdminApprovalVO)
	 * 
	 */
	public void reviewRegisterService(AdminApprovalVO adminApprovalVO) {
	    ServiceFacade serviceFacade = serviceLocator.getFacadeService(ServiceFacade.class);
	    UserServiceCompleteStepVO completeStepVO = serviceFacade.reviewRegisterService(adminApprovalVO);
	    try{
			UserServiceWF userServiceWF = serviceLocator.getWorkflowService(UserServiceWF.class);
			userServiceWF.completeNonClientWFStep(completeStepVO);
	    }catch(Throwable t){
	    	serviceFacade.undoReviewRegisterService(adminApprovalVO);
	    	throw new AdminWorkflowException(t);
	    }
	}

	/**
	 * findServiceListByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceListByUserName(java.lang.String)
	 * 
	 */
	public List<ServiceVO> findServiceListByUserName(String userName) {
		return this.serviceLocator.getFacadeService(ServiceFacade.class).findServiceListByUserName(userName);
	}

	/**
	 * getServiceListByPagingVO
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#getServiceListByPagingVO(ae.pcfc.etks.online.common.vo.PagingVO)
	 * 
	 */
	public List<ServiceVO> getServiceListByPagingVO(PagingVO pagingVO) {
		return this.serviceLocator.getFacadeService(ServiceFacade.class).getServicesListByPagingVO(pagingVO);
	}

	/**
	 * registerEmployee
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#registerEmployee(ae.pcfc.etks.online.common.vo.UserVO, ae.pcfc.etks.online.common.vo.UserServiceVO)
	 * 
	 */
	public void registerEmployee(UserVO userVO, UserServiceVO userServiceVO){
		this.serviceLocator.getFacadeService(ServiceFacade.class).registerEmployee(userVO, userServiceVO);
	}
	/**
	 * updateEmployee
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#updateEmployee(ae.pcfc.etks.online.common.vo.UserVO, ae.pcfc.etks.online.common.vo.UserServiceVO)
	 * 
	 */
	public void updateEmployee(UserVO userVO, UserServiceVO userServiceVO){
		this.serviceLocator.getFacadeService(ServiceFacade.class).updateEmployee(userVO, userServiceVO);
	}

	/**
	 * findClientRolesByRoleIds
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findClientRolesByRoleIds(java.util.List)
	 * 
	 */
	public List<ClientRoleVO> findClientRolesByRoleIds(List<Integer> roleIds) {
		// TODO Auto-generated method stub
		return this.serviceLocator.getFacadeService(ServiceFacade.class).findClientRolesByRoleIds(roleIds);
	}

	/**
	 * findServiceDepartmentsByDepartmentId
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceDepartmentsByDepartmentId(java.util.List)
	 * 
	 */
	public List<DepartmentVO> findServiceDepartmentsByDepartmentId(
			List<String> departmentCodes) {
		// TODO Auto-generated method stub
		return this.serviceLocator.getFacadeService(ServiceFacade.class).findServiceDepartmentsByDepartmentId(departmentCodes);
		
	}

	/**
	 * findServiceRolesByServiceType
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceRolesByServiceType(java.lang.String)
	 * 
	 */
	public List<ServiceRoleVO> findServiceRolesByServiceType(String serviceType) {
		// TODO Auto-generated method stub
		return this.serviceLocator.getFacadeService(ServiceFacade.class).findServiceRolesByServiceType(serviceType);
	}

	/**
	 * registerInternalStaff
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#registerInternalStaff(ae.pcfc.etks.online.common.vo.UserVO, java.util.List)
	 * 
	 */
	public void registerInternalStaff(UserVO userVO, List<Long> arrRoles) {
		// TODO Auto-generated method stub
		this.serviceLocator.getFacadeService(ServiceFacade.class).registerInternalStaff(userVO, arrRoles);		
	}

	/**
	 * findServiceRolesByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#findServiceRolesByUserName(java.lang.String)
	 * 
	 */
	public List<ServiceRoleVO> findServiceRolesByUserName(String strUserName) {
		// TODO Auto-generated method stub
		return this.serviceLocator.getFacadeService(ServiceFacade.class).findServiceRolesByUserName(strUserName);
	}

	/**
	 * updateInternalStaff
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.ServiceDelegate#updateInternalStaff(ae.pcfc.etks.online.common.vo.UserVO, java.util.List)
	 * 
	 */
	public void updateInternalStaff(UserVO userVO, List<Long> arrRoles) {
		// TODO Auto-generated method stub
		this.serviceLocator.getFacadeService(ServiceFacade.class).updateInternalStaff(userVO, arrRoles);
		
	}

	public List<ProcessVO> getProcessList() {
		return this.serviceLocator.getFacadeService(ServiceFacade.class).getProcessList();
	}

}
